<%@ page import="java.sql.*" %><%--
  Created by IntelliJ IDEA.
  User: 113
  Date: 2020/10/6
  Time: 14:14
  To change this temhplate use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>//加载数据库驱动//建立数据库连接 //创建Statement对象//执行查询  返回结果集    //遍历结果集数据

    <title>Title</title>
</head>
<body>
<%
    if(session.getAttribute("username")==null){
        //表示尚未登录
        out.print("你尚未登录，3秒跳转登录页面")；
        response.setHeader("refresh,3;url=");
    }
//加载数据库驱动
    Class.forName("com.mysql.jdbc.Driver");

    //建立数据库连接
    String url ="jdbc:mysql://localhost:3306/book";
    Connection connection = DriverManager.getConnection(url,"root","root");
    String sql ="select * from user shere username =?";
    PreparedStatement ps =connection.prepareStatement(sql);
    ps.setString(1,request.getParameter("username"));
    ps.setString(2,request.getParameter("password"));
    //String sql ="insert into user(username,password,gender) values (?,?,?)";
    ResultSet rs =ps.executeQuery();
    while (rs.next()){

        out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");

    }

    //创建Statement对象
    Statement stmt = connection.createStatement();

    String sql = "select *from user where username ='"+request.getParameter("username")+"'";

//执行查询  返回结果集
 ResultSet rs = stmt.executeQuery(sql);


    //遍历结果集数据
    while(rs.next()){

    out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");

    }


/*

Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/submit?user=root&password=root";
Connection connection= DriverManager.getConnection(url);
String sql="insert into user(userid,username,userpassword,usersex) values("+user.getUserid()+",'"+user.getUsername()+"','"+user.getUserpassword()+"',"+user.getUsersex()+")";//增
//String sql="update user set username='离开' where userid=4";//改
//String sql="delete from user where userid=1";//删
//String sql="select * from  user";//查

Statement stmt=connection.createStatement();
int count=stmt.executeUpdate(sql);
if (count==0)
{
    out.print("失败");
}
else
{
    out.print("成功");
}
*/


%>

<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <h3 style="text-align:center">欢迎来到个人主页！</h3>
            <table class="table table-striped">
                <thead>
                <th>ID</th>
                <th>用户名</th>
                <th>性别</th>
                <th>年龄</th>
                </thead>
                <tbody>
                <%
                    while(rs.next()){

                        out.print(
                                "<td>"+rs.getString("id")
                                        +"</td><td>"+rs.getString("username")
                                        +"</td><td>"+rs.getString("gender")
                                        +"</td><td>"+rs.getString("age")
                                        +"</td>"
                                        +"<a href='delete.jsp?id=>"+rs.getString("id")+"'>删除</a>"
                                        +"&nbsp;<a href='edit.jsp?id="+rs.getString("id")+"'>修改</a>"+</td>"

                    );
                        out.print("</tr>");
                    }
                    }
                %>
                </tbody>
        </div>
    </div>
</div>
</body>
</html>
